package com.argrace.smart.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.argrace.smart.R;
import com.argrace.smart.base.BaseActivity;
import com.argrace.smart.base.ConstantConfigure;
import com.argrace.smart.entity.MineZigBeeDeviceListEntity;
import com.argrace.smart.glide.GliderImageLoaderUtil;
import com.argrace.smart.model.LocalDb_AnJianTable;
import com.argrace.smart.model.LocalDb_QingJingTable;

import org.litepal.crud.DataSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018\2\20 0020.
 */

public class DeviceControl_FuKongActivity extends BaseActivity {
    private ImageView img;
    private TextView text1, text2, text3, text4, text5, text6;
    MineZigBeeDeviceListEntity bean;
    private LinearLayout ll1, ll2, ll3, ll4, ll5, ll6;
    private Map<String, String> model_imgurl = new HashMap<>();
    private int type = ConstantConfigure.TYPE_CONTROL;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_devicecontrol_fukong);
        init();
    }

    private void init() {
        bean = (MineZigBeeDeviceListEntity) getIntent().getSerializableExtra("deviceinfo");
        type = getIntent().getIntExtra("type", ConstantConfigure.TYPE_CONTROL);
        setTitle(true, false, bean.getName());
        showRight(false, R.mipmap.head_title_tick);
        setLeftandRightClickListeners(null, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DeviceControl_FuKongActivity.this, DeviceModifyActivity.class);
                intent.putExtra("uuid", bean.getUuid());
                intent.putExtra("model", bean.getModel());
                startActivity(intent);
            }
        });
        setView();
        getDeviceDetailInfo();
    }

    private void setView() {
        img = (ImageView) findViewById(R.id.activity_devicecontrol_img);
        text1 = (TextView) findViewById(R.id.activity_devicecontrol_fukong_text1);
        text2 = (TextView) findViewById(R.id.activity_devicecontrol_fukong_text2);
        text3 = (TextView) findViewById(R.id.activity_devicecontrol_fukong_text3);
        text4 = (TextView) findViewById(R.id.activity_devicecontrol_fukong_text4);
        text5 = (TextView) findViewById(R.id.activity_devicecontrol_fukong_text5);
        text6 = (TextView) findViewById(R.id.activity_devicecontrol_fukong_text6);
        ll1 = (LinearLayout) findViewById(R.id.activity_devicecontrol_fukong_ll1);
        ll2 = (LinearLayout) findViewById(R.id.activity_devicecontrol_fukong_ll2);
        ll3 = (LinearLayout) findViewById(R.id.activity_devicecontrol_fukong_ll3);
        ll4 = (LinearLayout) findViewById(R.id.activity_devicecontrol_fukong_ll4);
        ll5 = (LinearLayout) findViewById(R.id.activity_devicecontrol_fukong_ll5);
        ll6 = (LinearLayout) findViewById(R.id.activity_devicecontrol_fukong_ll6);
        String imgurl = null;
        if (bean.getModel().equals(ConstantConfigure.MODEL_SANWEIFUKONG)) {
            imgurl = ConstantConfigure.IMGURL_SANWEIQINGCHUFUKONG_005F1E14;
            ll1.setVisibility(View.VISIBLE);
            ll2.setVisibility(View.VISIBLE);
            ll3.setVisibility(View.VISIBLE);
            ll4.setVisibility(View.GONE);
            ll5.setVisibility(View.GONE);
            ll6.setVisibility(View.GONE);
            ll1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DeviceControl_FuKongActivity.this, QingJingListActivity.class);
                    startActivityForResult(intent, ConstantConfigure.REQUESTCODE_SWITCH1);
                }
            });
            ll2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DeviceControl_FuKongActivity.this, QingJingListActivity.class);
                    startActivityForResult(intent, ConstantConfigure.REQUESTCODE_SWITCH2);
                }
            });
            ll3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DeviceControl_FuKongActivity.this, QingJingListActivity.class);
                    startActivityForResult(intent, ConstantConfigure.REQUESTCODE_SWITCH3);
                }
            });
        }
        if (bean.getModel().equals(ConstantConfigure.MODEL_LIANGWEIFUKONG)) {
            imgurl = ConstantConfigure.IMGURL_LIANGWEIQINGCHUFUKONG_005f1dcc;
            ll1.setVisibility(View.VISIBLE);
            ll2.setVisibility(View.VISIBLE);
            ll3.setVisibility(View.GONE);
            ll4.setVisibility(View.GONE);
            ll5.setVisibility(View.GONE);
            ll6.setVisibility(View.GONE);
            ll1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DeviceControl_FuKongActivity.this, QingJingListActivity.class);
                    startActivityForResult(intent, ConstantConfigure.REQUESTCODE_SWITCH1);
                }
            });
            ll2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DeviceControl_FuKongActivity.this, QingJingListActivity.class);
                    startActivityForResult(intent, ConstantConfigure.REQUESTCODE_SWITCH2);
                }
            });
        }
        if (bean.getModel().equals(ConstantConfigure.MODEL_YIWEIFUKONG)) {
            imgurl = ConstantConfigure.IMGURL_YIWEIQINGCHUFUKONG_005F1E12;
            ll1.setVisibility(View.VISIBLE);
            ll2.setVisibility(View.GONE);
            ll3.setVisibility(View.GONE);
            ll4.setVisibility(View.GONE);
            ll5.setVisibility(View.GONE);
            ll6.setVisibility(View.GONE);
            ll1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DeviceControl_FuKongActivity.this, QingJingListActivity.class);
                    startActivityForResult(intent, ConstantConfigure.REQUESTCODE_SWITCH1);
                }
            });
        }

        if (bean.getModel().equals(ConstantConfigure.MODEL_CHANGJINGKAIGUAN)) {
            imgurl = ConstantConfigure.IMGURL_CHANGJINGKAIGUAN_005F1DCD;
            ll1.setVisibility(View.VISIBLE);
            ll2.setVisibility(View.VISIBLE);
            ll3.setVisibility(View.VISIBLE);
            ll4.setVisibility(View.VISIBLE);
            ll5.setVisibility(View.VISIBLE);
            ll6.setVisibility(View.VISIBLE);
            ll1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DeviceControl_FuKongActivity.this, QingJingListActivity.class);
                    startActivityForResult(intent, ConstantConfigure.REQUESTCODE_SWITCH1);
                }
            });
            ll2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DeviceControl_FuKongActivity.this, QingJingListActivity.class);
                    startActivityForResult(intent, ConstantConfigure.REQUESTCODE_SWITCH2);
                }
            });
            ll3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DeviceControl_FuKongActivity.this, QingJingListActivity.class);
                    startActivityForResult(intent, ConstantConfigure.REQUESTCODE_SWITCH3);
                }
            });
            ll4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DeviceControl_FuKongActivity.this, QingJingListActivity.class);
                    startActivityForResult(intent, ConstantConfigure.REQUESTCODE_SWITCH4);
                }
            });
            ll5.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DeviceControl_FuKongActivity.this, QingJingListActivity.class);
                    startActivityForResult(intent, ConstantConfigure.REQUESTCODE_SWITCH5);
                }
            });
            ll6.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DeviceControl_FuKongActivity.this, QingJingListActivity.class);
                    startActivityForResult(intent, ConstantConfigure.REQUESTCODE_SWITCH6);
                }
            });

        }
        if (imgurl != null) {
            GliderImageLoaderUtil.load(this, imgurl, img);
        }
    }

    //查询设备存储的场景信息
    private void getDeviceDetailInfo() {

        List<LocalDb_AnJianTable> beans = DataSupport.where("uuid = ?", bean.getUuid()).find(LocalDb_AnJianTable.class);
        if (beans != null && beans.size() > 0) {
            //如果有数据，查询情景表
            for (int i = 0; i < beans.size(); i++) {
                LocalDb_QingJingTable bean = DataSupport.find(LocalDb_QingJingTable.class, beans.get(i).getQingJingTable_id());
                fillData(beans.get(i), bean);
            }
        }
    }

    private void fillData(LocalDb_AnJianTable bean_anjian, LocalDb_QingJingTable bean_qingjing) {
        switch (bean_anjian.getKey()) {
            case "1":
                text1.setText(bean_qingjing.getName());
                break;
            case "2":
                text2.setText(bean_qingjing.getName());
                break;
            case "3":
                text3.setText(bean_qingjing.getName());
                break;
            case "4":
                text4.setText(bean_qingjing.getName());
                break;
            case "5":
                text5.setText(bean_qingjing.getName());
                break;
            case "6":
                text6.setText(bean_qingjing.getName());
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            String name = data.getStringExtra("name");
            int id = data.getIntExtra("id", 0);
            if (requestCode == ConstantConfigure.REQUESTCODE_SWITCH1) {
                text1.setText(name);
                LocalDb_AnJianTable anJianTable = new LocalDb_AnJianTable();
                anJianTable.setUuid(bean.getUuid());
                anJianTable.setKey("1");
                anJianTable.setQingJingTable_id(id);
                int result = anJianTable.updateAll("uuid = ? and key = ?", bean.getUuid(), "1");
                if (result == 0) {
                    anJianTable.save();
                }
            }
            if (requestCode == ConstantConfigure.REQUESTCODE_SWITCH2) {
                text2.setText(name);
                LocalDb_AnJianTable anJianTable = new LocalDb_AnJianTable();
                anJianTable.setUuid(bean.getUuid());
                anJianTable.setKey("2");
                anJianTable.setQingJingTable_id(id);
                int result = anJianTable.updateAll("uuid = ? and key = ?", bean.getUuid(), "2");
                if (result == 0) {
                    anJianTable.save();
                }
            }
            if (requestCode == ConstantConfigure.REQUESTCODE_SWITCH3) {
                text3.setText(name);
                LocalDb_AnJianTable anJianTable = new LocalDb_AnJianTable();
                anJianTable.setUuid(bean.getUuid());
                anJianTable.setKey("3");
                anJianTable.setQingJingTable_id(id);
                int result = anJianTable.updateAll("uuid = ? and key = ?", bean.getUuid(), "3");
                if (result == 0) {
                    anJianTable.save();
                }
            }
            if (requestCode == ConstantConfigure.REQUESTCODE_SWITCH4) {
                text4.setText(name);
                LocalDb_AnJianTable anJianTable = new LocalDb_AnJianTable();
                anJianTable.setUuid(bean.getUuid());
                anJianTable.setKey("4");
                anJianTable.setQingJingTable_id(id);
                int result = anJianTable.updateAll("uuid = ? and key = ?", bean.getUuid(), "4");
                if (result == 0) {
                    anJianTable.save();
                }
            }
            if (requestCode == ConstantConfigure.REQUESTCODE_SWITCH5) {
                text5.setText(name);
                LocalDb_AnJianTable anJianTable = new LocalDb_AnJianTable();
                anJianTable.setUuid(bean.getUuid());
                anJianTable.setKey("5");
                anJianTable.setQingJingTable_id(id);
                int result = anJianTable.updateAll("uuid = ? and key = ?", bean.getUuid(), "5");
                if (result == 0) {
                    anJianTable.save();
                }
            }
            if (requestCode == ConstantConfigure.REQUESTCODE_SWITCH6) {
                text6.setText(name);
                LocalDb_AnJianTable anJianTable = new LocalDb_AnJianTable();
                anJianTable.setUuid(bean.getUuid());
                anJianTable.setKey("6");
                anJianTable.setQingJingTable_id(id);
                int result = anJianTable.updateAll("uuid = ? and key = ?", bean.getUuid(), "6");
                if (result == 0) {
                    anJianTable.save();
                }
            }
            getDeviceDetailInfo();

        }

    }
}
